package com.demo.workSpace;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import android.app.Activity;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

public class Demo extends Activity {
	private ArrayList< HashMap<String, Object> > mAppsInfo;
	private GridView mView1;
	private GridView mView2;
	
	private SimpleAdapter mAdapter1;
	private SimpleAdapter mAdapter2;
	private WorkSpace mWorkspace;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWorkspace = new WorkSpace(this, null);
//        setContentView(mWorkspace);
        init();
    }
    
    private void init(){
    	initData();
    	initView();
    }
    
    private void initData(){
    	mAppsInfo = new ArrayList< HashMap<String, Object> >();
    	String appName;
    	Drawable appIcon;
    	List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
    	int size = packages.size();
    	//two screen ,each 15 
    	size = size > 30 ? 30 : size;
	    for(int i = 0; i < size; i++) { 
	        PackageInfo packageInfo = packages.get(i); 
        	HashMap<String, Object> map = new HashMap<String, Object>();
        	appName = 
        		packageInfo.applicationInfo.loadLabel(getPackageManager()).toString(); 
	        appIcon = 
	        	packageInfo.applicationInfo.loadIcon(getPackageManager());
	        //drawable cast to bitmap
	        Bitmap bitmap = ((BitmapDrawable)appIcon).getBitmap();
	        map.put("itemImage", bitmap);
	        map.put("itemName", appName);
	        mAppsInfo.add(map);
//	        }
	    }
    }
    
    private void initView(){
    	LayoutInflater inflater = LayoutInflater.from(this);
    	View view1 = inflater.inflate(R.layout.grid_view, null);
    	View view2 = inflater.inflate(R.layout.grid_view, null);
    	
    	mView1 = (GridView)view1.findViewById(R.id.gridview);
    	mView2 = (GridView)view2.findViewById(R.id.gridview);
    	
    	mAdapter1 = instanceAdapter(0, 15);
    	mAdapter2 = instanceAdapter(15, 30);
    
    	mView1.setAdapter(mAdapter1);
    	mView2.setAdapter(mAdapter2);
    	
    	mView2.setBackgroundColor(Color.alpha(255));
    	
    	binderView(mAdapter1);
    	binderView(mAdapter2);
//    	mWorkspace.setPadding(100, 0, 100, 0);
    	setContentView(mWorkspace);
    	
    	mWorkspace.addView(mView1);
    	mWorkspace.addView(mView2);
    	
    }
    
    private SimpleAdapter instanceAdapter(int start, int end){
    	return new SimpleAdapter(this,
								 mAppsInfo.subList(start, end),
								 R.layout.item,
								 new String[]{"itemImage", "itemName"}, 
								 new int[]{R.id.ItemImage, R.id.ItemText});
    }
    
    //simpleAdapter 显示图片如果不是本地的可以用此方法
    private void binderView(SimpleAdapter adapter){
    	adapter.setViewBinder(new ViewBinder() {    
            public boolean setViewValue(  
                                View view,   
                                Object data,    
                             String textRepresentation) {    
                //判断是否为我们要处理的对象    
                if(view instanceof ImageView  && data instanceof Bitmap){    
                    ImageView iv = (ImageView) view;    
                    iv.setImageBitmap((Bitmap) data);    
                    return true;    
                }else    
                return false;    
            }    
        }); 
    }
}